/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-07-14 15:10:40
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-12 23:54:43
 */
import {getIcons, nodeColors} from '../../utils';

const LEGEND_ID = "topFloorId";
export const initData = (sourceData, associateType) => {
  let data = {nodes: [], edges: sourceData.edges};
  let moduleMap = new Map();
  let _color = null;
  sourceData.nodes.map(item=> {
    const {cmModuleId, moduleName, moduleIcon} = item;
    const icon = getIcons(moduleIcon||'icon-default');
    const uniq = item[LEGEND_ID];
    if (uniq && moduleMap.get(uniq) === undefined) {
      moduleMap.set(uniq, item);
    }
    const cid = moduleMap.size - 1;
    _color = nodeColors[cid % nodeColors.length];
    data.nodes.push({
        ...item,
        moduleId: cmModuleId,
        id: cmModuleId,
        label: moduleName,
        icon: {
          fontFamily: 'iconfont',
          text: icon
        },
        style: {
          fill: _color,
          stroke: _color
        },
        cluster: uniq
    });
  })

  data.edges.forEach((edge, index) => {
    const cid = moduleMap.get(edge[LEGEND_ID]);
    const _color = nodeColors[cid % nodeColors.length];
    Object.assign(edge, {
      source: edge.srcModuleId,
      target: edge.destModuleId,
      label: associateType?.[edge.associateType]??edge.associateType,
      style: {
        stroke:  _color,
        endArrow: {
          fill: _color,
        }
      },
      labelCfg: {
        style: {
          fill: _color,
          background: {
            stroke: _color,
          }
      }
      }
    })
  })

  console.log(data)
  return {data, moduleMap}
}

const getFilterFunctions = (data)=> {
  let obj = {};
  data.map(item => (
    obj[item.id] = new Function('d',`if(d['${LEGEND_ID}'] == '${item.id}') return true; return false`)
  ))
  return obj;
}

const moduleClassificationGroup = [
  ['biz_topology_group'],
  [],
  []
];
const moduleGroup = [
  ['app_system', 'biz', 'service', 'set'],
  ['server', 'metal_server', 'phy_server'],
  ['volume', 'network', 'router', 'floatip']
]
const moduleGroupFlat = moduleGroup.flat();
const moduleClassificationGroupFlat = moduleClassificationGroup.flat();

export const getLayout = () => {
  const arr = [];
  let i = 0;
  moduleGroup.map((item, index) => {
    arr.push({
      type: 'grid',
      begin: [20 + i * 180, 20],
      nodesFilter: (node) => !item.length || item.includes(node.moduleCode) || moduleClassificationGroup[index].includes(node.topFloorCode),
      cols: 1,
      preventOverlap: true,
      preventOverlapPadding: 120,
      workerEnabled: true,
    })
    i++;
  })
  arr.push({
    type: 'grid',
    begin: [20 + i * 240 , 0],
    nodesFilter: (node) => !(moduleGroupFlat.includes(node.moduleCode) || moduleClassificationGroupFlat.includes(node.topFloorCode)),
    preventOverlap: true,
    preventOverlapPadding: 80,
    workerEnabled: true
  })
  return arr;
}